<?php

class Bootstrap extends Zend_Application_Bootstrap_Bootstrap {
    
    public static function _phpErrorHandler($errno, $errstr, $errfile, $errline) {
        $message = sprintf("Php Error is Triggered\n=======================================\n\nError No: %s\nError Message: %s\n\nFile: %s\nLine: %s\n\n\n", 
                        $errno, $errstr, $errfile, $errline);
        
        //$message = nl2br($message);
        echo '<pre>'.$message.'</pre>';
        //$e = new Exception($message);
        //throw $e;
    }
    //=================================================================================
    
    protected function _initErrorHandler() {
        //$oldErrorHandler = set_error_handler ( array('Bootstrap', '_phpErrorHandler'), E_ALL | E_STRICT );
    }
    //=================================================================================
    
    protected function _initI18n() {
        
    }
    //=================================================================================
    
	/**
	 * Bootstrap autoloader for application resources
	 * @return Zend_Application_Module_Autoloader
	 */
	protected function _initAutoload() {
	    $autoLoader = Zend_Loader_Autoloader::getInstance();
        $resourceLoader = new Zend_Loader_Autoloader_Resource(array(
            'basePath'        => APPLICATION_PATH,
            'namespace'        => '',
            'resourceTypes'    => array(
                'model' => array(
                    'path'        => 'models/',
                    'namespace'    => 'Model_'
                )
            )
        ));
        
 		return $autoLoader;
    }
    //=================================================================================
    
    /**
     * 
     */
    protected function _initDoctrine()
    {
        $this->getApplication()->getAutoloader()
            ->pushAutoloader(array('Doctrine', 'autoload'));
        spl_autoload_register(array('Doctrine', 'modelsAutoload'));
        
        $doctrineConfig = $this->getOption('doctrine');
        $manager = Doctrine_Manager::getInstance();
        //$manager->setCharset('cp1251');
        //$manager->setCharset('utf-8');
        $manager->setAttribute(Doctrine::ATTR_AUTO_ACCESSOR_OVERRIDE, true);
        $manager->setAttribute(
          Doctrine::ATTR_MODEL_LOADING,
          $doctrineConfig['model_autoloading']
        );

        Doctrine_Core::loadModels($doctrineConfig['models_path']);

        $conn = Doctrine_Manager::connection($doctrineConfig['dsn'],'doctrine');
        $conn->setAttribute(Doctrine::ATTR_USE_NATIVE_ENUM, true);
        
        //Doctrine_Query::query("SET NAMES utf8");
        
        return $conn;
    }
    
    protected function _initConfig() {
        $config = new Zend_Config($this->getOptions(), true);
        Zend_Registry::set('config', $config);
        
        return $config;
    }
    //=================================================================================
    /*   
    protected function _initDoctype() {
        $this->bootstrap('view');
        $view = $this->getResource('view');
        $view->doctype('XHTML1_STRICT');
        
        $view->addHelperPath('views/helpers/', 'Views_Helpers');
    }
    //=================================================================================
    */
    protected function _initNavigation() {
        $this->bootstrap('layout');
        $layout = $this->getResource('layout');
        $view = $layout->getView();
        
        $config = new Zend_Config_Xml(APPLICATION_PATH.'/configs/navigation.xml', 'nav');
        $navigation = new Zend_Navigation($config);
        $view->navigation($navigation);
    }
    //=================================================================================
    
    protected function _initRoutes() {
        if(Zend_Registry::isRegistered('Zend_Locale')) {
            $this->__initRoutesWithLocale();
            
        }
        else {
            $this->__initRoutesWithoutLocale();
        }
    }
    //=================================================================================

    private function __initRoutesWithoutLocale() {
        
        /*
         * Route Definitions
         */
    	$myRoutes['login'] = new Zend_Controller_Router_Route('login',
    					array('controller' => 'index',
                             'action' => 'login'));
    	 
    	$myRoutes['logout'] = new Zend_Controller_Router_Route('logout',
    					array('controller' => 'index',
                             'action' => 'logout'));
    					
    					
    	$myRoutes['projectEdit'] = new Zend_Controller_Router_Route('projects/:id',
                        array('controller' => 'projects',
        					'action'     => 'editProject'));
        
        $myRoutes['projectVirtualHosts'] = new Zend_Controller_Router_Route('projects/:id/virtual_hosts',
                        array('controller' => 'projects',
        					'action'     => 'EditVirtualHost'));
                        
        $myRoutes['projectDelete'] = new Zend_Controller_Router_Route('projects/delete/:id',
                        array('controller' => 'projects',
        					'action'     => 'delete'));
 
                        
        $myRoutes['userEdit'] = new Zend_Controller_Router_Route('users/:id',
                        array('controller' => 'users',
    	        			'action'	=> 'add',
    	        			'id'		=> 'id'));
        
        
        /*
         * Add Routes
         */
        $front = Zend_Controller_Front::getInstance();
    	$router = $front->getRouter();
        $router->addRoutes($myRoutes);
    }
    //=================================================================================
    
    private function __initRoutesWithLocale() {
        $front = Zend_Controller_Front::getInstance();
    	$router = $front->getRouter();

	    $locale = Zend_Registry::get('Zend_Locale');

	    $routeLang = new Zend_Controller_Router_Route (
        	':lang',
    	    array(
            	'lang' => $locale->getLanguage(),
    	    ),
    	    array('lang' => '[a-z]{2}')
	    );
    	    
    	$routeDefault = new Zend_Controller_Router_Route_Module(
	    	array(),
	    	$front->getDispatcher(),
	    	$front->getRequest()
    	);

    	$routeLogin = new Zend_Controller_Router_Route(':lang/login',
    					array('controller' => 'index',
                             'action' => 'login',
                             'lang'=>$locale->getLanguage()));
    	 
    	$routeLogout = new Zend_Controller_Router_Route(':lang/logout',
    					array('controller' => 'index',
                             'action' => 'logout',
                             'lang'=>$locale->getLanguage()));
    	$router->addRoute('login', $routeLogin);
    	$router->addRoute('logout',$routeLogout);
    	
    	$router->addRoute('user-view', new Zend_Controller_Router_Route(
    	    ':lang/users/:id', array(
    	        'controller' => 'users',
    	        'action'	=> 'add',
    	        'id'		=> 'id'
    			
    	    )
    	));
    	
    	$router->addRoute('realmlistservers', new Zend_Controller_Router_Route(':lang/servers/realm/:realm/',
    					array(
      						'controller'	=> 'servers',
      						'action'		=> 'list', 
      						'lang'			=> $locale->getLanguage()
    					)));
    	$front->registerPlugin(new Util_Controller_Plugin_Language());
    }
}

